import requests
import logging
from sdk.views import HOST, CORPID, AGENTID, SECRET, getAccessToken

logger = logging.getLogger("log")


def getUserIdByCode(usercode):
    code = 0
    msg = "success"
    data = {}
    apiName = "获取访问用户身份"
    logger.info(f"接口>>>{apiName}>>>开始调用")
    ACCESS_TOKEN = getAccessToken()
    if ACCESS_TOKEN == False:
        code = 1
        msg = "获取access_token失败"
        apiOutput = f"1.code<{code}>; 2.msg<{msg}>;"
        logger.info(f"接口>>>{apiName}>>>执行失败>>>{apiOutput}")
        return {"code": code, "msg": msg, "data": data}
    url = HOST + "auth/getuserinfo?access_token=" + ACCESS_TOKEN
    url += "&code=" + usercode
    apiInput = f"1.access_token<ACCESS_TOKEN>;2.usercode<{usercode}>;"
    logger.info(f"接口>>>{apiName}>>>入参>>>{apiInput}")
    try:
        response = requests.get(url)
        responseJson = response.json()
        print("responseJson")
        print(responseJson)
        if responseJson["errcode"] != 0:
            code = responseJson["errcode"]
            msg = responseJson["errmsg"]
            apiOutput = f"1.code<{code}>; 2.msg<{msg}>;"
            logger.info(f"接口>>>{apiName}>>>执行失败>>>{apiOutput}")
        else:
            userid = responseJson["userid"]
            data["userid"] = userid
            try:
                user_ticket = responseJson["user_ticket"]
            except Exception:
                user_ticket = ""
            data["user_ticket"] = user_ticket
            apiOutput = f"1.code<{code}>;2.msg<{msg}>;3.userid<{userid}>;4.user_ticket<{user_ticket}>;"
            logger.info(f"接口>>>{apiName}>>>执行成功>>>{apiOutput}")
    except Exception as e:
        print("未知异常")
        print(e)
        code = 1
        msg = "未知异常"
        apiOutput = f"1.code<{code}>; 2.msg<{msg}>;"
        logger.info(f"接口>>>{apiName}>>>执行失败>>>{apiOutput}")
    ret = {"code": code, "msg": msg, "data": data}
    return ret


def getUserDetailByTicket(userticket):
    code = 0
    msg = "SUCCESS"
    data = {}
    apiName = "获取访问用户敏感信息"
    logger.info(f"接口>>>{apiName}>>>开始调用")
    ACCESS_TOKEN = getAccessToken()
    if ACCESS_TOKEN == False:
        code = 1
        msg = "获取access_token失败"
        apiOutput = f"1.code<{code}>; 2.msg<{msg}>;"
        logger.info(f"接口>>>{apiName}>>>执行失败>>>{apiOutput}")
        return {"code": code, "msg": msg, "data": data}
    url = HOST + "auth/getuserdetail?access_token=" + ACCESS_TOKEN
    d = {"user_ticket": userticket}
    apiInput = f"1.access_token<ACCESS_TOKEN>;2.userticket<{userticket}>;"
    logger.info(f"接口>>>{apiName}>>>入参>>>{apiInput}")
    try:
        response = requests.post(url, json=d)
        responseJson = response.json()
        print("responseJson")
        print(responseJson)
        errcode = responseJson["errcode"]
        if errcode != 0:
            code = responseJson["errcode"]
            msg = responseJson["errmsg"]
            apiOutput = f"1.code<{code}>; 2.msg<{msg}>;"
            logger.info(f"接口>>>{apiName}>>>执行失败>>>{apiOutput}")
        else:
            data["userid"] = responseJson["userid"]
            data["gender"] = responseJson["gender"]
            data["avatar"] = responseJson["avatar"]
            data["qr_code"] = responseJson["qr_code"]
            data["mobile"] = responseJson["mobile"]
            data["email"] = responseJson["email"]
            data["biz_mail"] = responseJson["biz_mail"]
            data["address"] = responseJson["address"]
            apiOutput = f"1.code<{code}>;2.msg<{msg}>;"
            logger.info(f"接口>>>{apiName}>>>执行成功>>>{apiOutput}")
    except Exception as e:
        code = 1
        msg = "未知异常"
        apiOutput = f"1.code<{code}>; 2.msg<{msg}>;"
        logger.info(f"接口>>>{apiName}>>>执行失败>>>{apiOutput}")
    ret = {"code": code, "msg": msg, "data": data}
    return ret


# 读取成员
def getUserById(userid):
    code = 0
    msg = "OK"
    data = {}
    apiName = "读取成员"
    logger.info(f"接口>>>{apiName}>>>开始调用")
    ACCESS_TOKEN = getAccessToken()
    if ACCESS_TOKEN == False:
        code = 1
        msg = "获取access_token失败"
        apiOutput = f"1.code<{code}>; 2.msg<{msg}>;"
        logger.info(f"接口>>>{apiName}>>>执行失败>>>{apiOutput}")
        return {"code": code, "msg": msg, "data": data}
    url = HOST + "user/get"
    url += "?access_token=" + ACCESS_TOKEN
    url += "&userid=" + userid
    apiInput = f"1.access_token<{ACCESS_TOKEN}>;2.userid<{userid}>;"
    logger.info(f"接口>>>{apiName}>>>入参>>>{apiInput}")
    try:
        response = requests.get(url)
        responseJson = response.json()
        print("responseJson")
        print(responseJson)
        errcode = responseJson["errcode"]
        if errcode != 0:
            code = responseJson["errcode"]
            msg = responseJson["errmsg"]
            apiOutput = f"1.code<{code}>; 2.msg<{msg}>;"
            logger.info(f"接口>>>{apiName}>>>执行失败>>>{apiOutput}")
        else:
            msg = "success"
            data["name"] = responseJson["name"]
            data["departmentLst"] = responseJson["department"]
            apiOutput = f"1.code<{code}>;2.msg<{msg}>;"
            logger.info(f"接口>>>{apiName}>>>执行成功>>>{apiOutput}")
    except Exception as e:
        code = 1
        msg = "未知异常"
        apiOutput = f"1.code<{code}>; 2.msg<{msg}>;"
        logger.info(f"接口>>>{apiName}>>>执行失败>>>{apiOutput}")
    ret = {"code": code, "msg": msg, "data": data}
    return ret


# 获取单个部门详情
def getDeptmentById(id):
    code = 0
    msg = "OK"
    data = {}
    apiName = "获取单个部门详情"
    logger.info(f"接口>>>{apiName}>>>开始调用")
    ACCESS_TOKEN = getAccessToken()
    if ACCESS_TOKEN == False:
        code = 1
        msg = "获取access_token失败"
        apiOutput = f"1.code<{code}>; 2.msg<{msg}>;"
        logger.info(f"接口>>>{apiName}>>>执行失败>>>{apiOutput}")
        return {"code": code, "msg": msg, "data": data}
    url = HOST + "department/get"
    url += "?access_token=" + ACCESS_TOKEN
    url += "&id=" + id
    apiInput = f"1.access_token<{ACCESS_TOKEN}>;2.id<{id}>;"
    logger.info(f"接口>>>{apiName}>>>入参>>>{apiInput}")
    try:
        response = requests.get(url)
        responseJson = response.json()
        print("responseJson")
        print(responseJson)
        errcode = responseJson["errcode"]
        if errcode != 0:
            code = responseJson["errcode"]
            msg = responseJson["errmsg"]
            apiOutput = f"1.code<{code}>; 2.msg<{msg}>;"
            logger.info(f"接口>>>{apiName}>>>执行失败>>>{apiOutput}")
        else:
            msg = "success"
            data["name"] = responseJson["name"]
            data["parentid"] = responseJson["parentid"]
            apiOutput = f"1.code<{code}>;2.msg<{msg}>;"
            logger.info(f"接口>>>{apiName}>>>执行成功>>>{apiOutput}")
    except Exception as e:
        code = 1
        msg = "未知异常"
        apiOutput = f"1.code<{code}>; 2.msg<{msg}>;"
        logger.info(f"接口>>>{apiName}>>>执行失败>>>{apiOutput}")
    ret = {"code": code, "msg": msg, "data": data}
    return ret
